/**
 * @author jouyang
 */

var QS_RightMenu = function(option){
	this.option = {
		preventDefault : true,
		preventForms : true	
	}
	this.menuMap = {};
	$.extend(true,this.option,option);
	this.init();
}
QS_RightMenu.prototype = {
	init:function(){
		$(document).bind("contextmenu.QS_RightMenu",this,this.showMenu);
		$(document).bind("click.QS_RightMenu",this,this.hideMenu);
	},
	clear:function(){
		$(document).unbind("contextmenu.QS_RightMenu",this.showMenu);
		$(document).unbind("click.QS_RightMenu",this.hideMenu);
	},
	registerMenu:function(containerId,data,callback){//containerClassName is unique.
		var str = '<ul class ="rtrm-ot">';
		for(var i=0;i<data.length;i++){
			
			str+='<li class="rtrm-out"><a '+(data[i].subMenu?'class="rtrm-arrow"':'')+'>'+data[i].name+'</a>';
			if(data[i].subMenu){
				str+='<ul>'
				for(var j=0;j<data[i].subMenu.length;j++){
					str+='<li><a class="selectable">'+data[i].subMenu[j].name+'</a></li>';
				}
				str+='</ul>'
			}
			str+='</li>';
		}
		str += "</ul>";
		var $menu = $(str).appendTo($("#"+containerId)).hide();
		this.menuMap[containerId]={menu:$menu};
		if(typeof(callback)== "function"){
			$menu.bind("click.QS_RightMenu",function(e){
				var t =$(e.target)
				if(t.hasClass("selectable")){ // 
					var v = t.attr("value"), c=t.attr("cate");
					t.closest("ul").find("li a.on").removeClass("on");
					t.addClass("on");
					callback(c,v);
				}
			});
		}
		
	},
	unregisterMenu:function(containerId){
		delete this.menuMap[containerId];
		return;
	},
	
	showMenu:function(e){
		var self = e.data;
		self.hideMenu(e);
		var target = $(e.target), t,$con;
		for(var o in self.menuMap){
			$con = $("#"+o);
			t = target.closest("#"+o,$con);
			if(t&&t.length){
				self.attachedMenu = self.menuMap[o].menu;
				var pos = self._getPostion(e,$con);
				self.menuMap[o].menu.css({left:pos.left,top:pos.top})
				self.menuMap[o].menu.show();
				break;
			}
		}
		return self._getReturnValue(e);
	},
	hideMenu:function(e){
		var self = e.data;
		if(self.attachedMenu !=  null){
			self.attachedMenu.hide();
		}
	},
	_getReturnValue : function (e) {
		var self = e.data;
		if (e.button != 1) {
			var tname = e.target.tagName.toLowerCase();
			if ((tname == "input" || tname == "textarea")) {
				if (!self.option.preventForms) {
					returnValue = true;
				} else {
					returnValue = false;
				}
			} else {
				if (!self.option.preventDefault) {
					returnValue = true;
				} else {
					returnValue = false;
				}
			}
		}

		return returnValue;

	},
	_getPostion:function(e,$container){
		var scrollx = 0;
		var scrolly = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			scrollx = window.pageXOffset;
			scrolly = window.pageYOffset;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			scrollx = document.documentElement.scrollLeft;
			scrolly = document.documentElement.scrollTop;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			scrollx = document.body.scrollLeft;
			scrolly = document.body.scrollTop;
		}
		var oSet = $container.offset();
		return{left:e.pageX + scrollx - oSet.left,top:e.pageY + scrolly - oSet.top};
	}

}
var rightMenu = new QS_RightMenu({});

